home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / capi326a.zip / CLIPPER.C < prev    next >
C/C++ Source or Header  |  1990-08-02  |  7KB  |  197 lines

  1. /*************************************************
  2.  **                                             **
  3.  **   clipper.c                                 **
  4.  **                                             **
  5.  **     SoftC (tm) Dbase Library                **
  6.  **       Diskette TOC Demo Program             **
  7.  **                                             **
  8.  **           Copyright (C) 1989, 1990 by       **
  9.  **               SoftC, Ltd.                   **
  10.  **               16820 3rd St NE               **
  11.  **               Ham Lake, MN 55304            **
  12.  **               (612) 434-6968                **
  13.  **                                             **
  14.  **             All rights reserved.            **
  15.  *************************************************/
  16.  
  17.  
  18. /*
  19.           The Diskette  TOC Program is a simple program which will create a
  20.           database (TOC.DBF)  and three  index files.  The database  record
  21.           contains the following fields:
  22.  
  23.  
  24.           field name     type length    description
  25.           ----------     ---- ------    -----------
  26.           NAME            C    64       file name
  27.           LENGTH          N    10.0     file size in bytes
  28.           DATE            D     8       file creation date
  29.           TIME            C     8       file creation time
  30.           ATTRIBUTE       C     3       file attribute (READ ONLY, HIDDEN)
  31.  
  32.  
  33.           The  index   files  created   are:   TOCNAME.NTX   (file   name),
  34.           TOCLNGTH.NTX (file  length), and  TOCDATE.NTX (file creation date
  35.           and time).
  36.  
  37.  
  38.           The program  uses the DOS functions "findfirst" and "findnext" to
  39.           step through  the  files  in  the  directory.  It  reformats  the
  40.           compressed file  date  ("mm/dd/yy")  and  time  ("hh:mm:ss")  and
  41.           places the  resultant data  into the  output  buffer.  After  all
  42.           fields have  been entered  it will  append a record to the end of
  43.           the database and add keys for each file found.
  44.  
  45.  
  46.           After all the files have been cataloged, the name will be displayed
  47.           in alphabetical order using TOCNAME.NDX.
  48.  
  49.  
  50.           The program  only works  on the  current directory.  It does  not
  51.           support any command line arguments, although it would be easy for
  52.           the user  to add  such support.  Very little  error  checking  is
  53.           performed.
  54.  
  55.  
  56.           The program  as it  stands has value only in the demonstration of
  57.           certain library  functions. However, it could form the basis of a
  58.           diskette cataloger, or ...
  59.  
  60. */
  61.  
  62.  
  63. #include <stdlib.h>
  64. #include <stdio.h>
  65. #include <string.h>
  66. #if defined(__TURBOC__)
  67. #include <dir.h>
  68. #else
  69. #include <dos.h>
  70. #endif
  71. #include <softc.h>
  72. #include <sc_base.h>
  73.  
  74. SC_FIELD fields[] = { "name",'c',64,0,
  75.                       "length",'n',10,0,
  76.                       "date",'d',8,0,
  77.                       "time",'c',8,0,
  78.                       "attribute",'n',3,0 };
  79.  
  80. void main ()
  81. {
  82. #if defined(__TURBOC__)
  83.   struct ffblk ffblk;
  84. #else
  85.   struct find_t ffblk;
  86. #endif
  87.   int r, w1, w2, w3, dbf, ntx1, ntx2, ntx3;
  88.   char work[9], *key, name[65], fname[13], fattrib;
  89.   long recno, fsize;
  90.   unsigned fdate, ftime;
  91.   double d;
  92.  
  93.   atexit(scdterm);  /* make sure that the terminate function is called */
  94.   scdinit(20,0);  /* initialize the database manager */
  95.  
  96. /* create the database file */
  97.   scddcreate("toc.dbf",5,fields,SC_DB3);
  98.   scddopenx(&dbf,"toc.dbf",0);      /* open database file */
  99.  
  100. /* create empty index file on file name field */
  101.   scdccreate("tocname.ntx",'c',"name",64,0);
  102.   scdcopenx(&ntx1,"tocname.ntx",0);   /* open index file */
  103.  
  104. /* create empty index file on file length field */
  105.   scdccreate("toclngth.ntx",'n',"length",10,0);
  106.   scdcopenx(&ntx2,"toclngth.ntx",0);    /* open index file */
  107.  
  108. /* create empty index file using expression "dtoc(date) + time" */
  109.   scdccreate("tocdate.ntx",'c',"dtoc(date) + time",16,0);
  110.   scdcopenx(&ntx3,"tocdate.ntx",0);   /* open index file */
  111.  
  112. /* begin DOS file search */
  113. #if defined(__TURBOC__)
  114.   findfirst("*.*",&ffblk,0);
  115. #else
  116.   _dos_findfirst("*.*",0,&ffblk);
  117. #endif
  118.   do {
  119. #if defined(__TURBOC__)
  120.     strcpy(fname,ffblk.ff_name);  /* file name */
  121.     fsize = ffblk.ff_fsize;       /* file size */
  122.     fdate = ffblk.ff_fdate;       /* file creation date */
  123.     ftime = ffblk.ff_ftime;       /* file creation time */
  124.     fattrib = ffblk.ff_attrib;    /* file attributes */
  125. #else
  126.     strcpy(fname,ffblk.name);     /* file name */
  127.     fsize = ffblk.size;           /* file size */
  128.     fdate = ffblk.wr_date;        /* file creation date */
  129.     ftime = ffblk.wr_time;        /* file creation time */
  130.     fattrib = ffblk.attrib;       /* file attributes */
  131. #endif
  132.  
  133.   /* ignore the files I am using */
  134.     if ((strcmp(fname,"TOC.DBF") != 0) &&
  135.         (strcmp(fname,"TOCNAME.NTX") != 0) &&
  136.         (strcmp(fname,"TOCLNGTH.NTX") != 0) &&
  137.         (strcmp(fname,"TOCDATE.NTX") != 0)) {
  138.  
  139.       scddfput(dbf,0,fname);  /* put file name in record */
  140.  
  141.       d = (double) fsize; /* put file size in record */
  142.       scddfput(dbf,1,&d);
  143.  
  144.       w1 = ((fdate & 0xFE00) >> 9) + 80;  /* add date to record */
  145.       w2 = (fdate & 0x01E0) >> 5;
  146.       w3 = fdate & 0x001F;
  147.       sprintf(work,"%02d/%02d/%02d",w2,w3,w1);
  148.       scddfput(dbf,2,work);
  149.  
  150.       w1 = (ftime & 0xF800) >> 11;  /* add file create time to record */
  151.       w2 = (ftime & 0x07E0) >> 5;
  152.       w3 = (ftime & 0x001F) << 1;
  153.       sprintf(work,"%02d:%02d:%02d",w1,w2,w3);
  154.       scddfput(dbf,3,work);
  155.  
  156.       d = (double) fattrib; /* put file attributes in record */
  157.       scddfput(dbf,4,&d);
  158.  
  159.       if (scddrput(dbf,&recno,SC_ADD) == SC_SUCCESS) {  /* write record */
  160.  
  161.         scdckadd(ntx1,fname,recno); /* add key to "name" index */
  162.  
  163.         scdckmake(dbf,ntx2,(void **) &key); /* add key to "length" index */
  164.         scdckadd(ntx2,key,recno);
  165.         free(key);
  166.  
  167.         scdckmake(dbf,ntx3,(void **) &key); /* add key to "dtoc(date) + time" index */
  168.         scdckadd(ntx3,key,recno);
  169.         free(key);
  170.       }
  171.     }
  172. #if defined(__TURBOC__)
  173.     r = findnext(&ffblk); /* get next file */
  174.   } while (r != -1);
  175. #else
  176.     r = _dos_findnext(&ffblk);
  177.   } while (r == 0);
  178. #endif
  179.  
  180.   scdcktop(ntx1,name,&recno); /* get first record in "name" index */
  181.  
  182.   do {
  183.     scddrget(dbf,recno);  /* get corresponding record */
  184.  
  185.     scddfget(dbf,0,name); /* get file name field */
  186.  
  187.     scddfget(dbf,1,&d);   /* get file size */
  188.     printf("%s %10.0lf\n",name,d);
  189.  
  190.     scdcknext(ntx1,name,&recno);  /* get next key */
  191.   } while (sc_code != SC_END);    /* keep doing this until no more keys */
  192.  
  193.   scddclose(dbf);   /* close data file */
  194.   scdcclose(ntx1);  /* close "name" index file */
  195.   scdcclose(ntx2);  /* close "length" index file */
  196.   scdcclose(ntx3);  /* close other index file */
  197. }